package com.yundasys.lab12java.java8;

import java.util.HashMap;
import java.util.Stack;

/**
 * @PACKAGE_NAME: com.yundasys.lab12java.java8
 * @NAME: StackTest
 * @USER: jiangta
 * @DATE: 2023/1/19
 * @TIME: 9:50 上午
 * @PROJECT_NAME: springboot-labs
 */
public class StackTest {

    public boolean isValid(String s) {
        HashMap<Character,Character> mappings = new HashMap<>();
        mappings.put(')','(');
        mappings.put('}','{');
        mappings.put('[',']');

        Stack<Character> stack = new Stack<>();
        final char[] chars = s.toCharArray();
        for(int i=0;i<chars.length;i++) {
            if(mappings.containsKey(chars[i])) {
                char topElement = stack.empty() ? '#' : stack.pop();
                if(topElement != mappings.get(chars[i])) {
                    return false;
                }
            } else {
                stack.push(chars[i]);
            }

        }
        return stack.isEmpty();
    }
}
